import wifiManager from '@ohos.wifiManager';
import hilog from '@ohos.hilog';
import { Log } from '@ohos/common/src/main/ets/default/utils/Log';

const TAG = "wifiManager"
export class WifiManager {
  public static wifiManager: WifiManager

  private constructor() {
  }

  public static getInstance(): WifiManager {
    if(!WifiManager.wifiManager) {
      WifiManager.wifiManager = new WifiManager()
    }
    return WifiManager.wifiManager
  }

  startScan() {
    Log.info(TAG, "isWifiActive:"+JSON.stringify(wifiManager.isWifiActive()))
    if(wifiManager.isWifiActive()) {
      try {
        wifiManager.startScan();
        setTimeout(() => {
          this.getScanInfoList()
        }, 3000)
      }catch(error){
        Log.info(TAG, "isWifiActive failed:" + JSON.stringify(error))
      }
    }

  }

  getScanInfoList() {
    try {
      let scanInfoList = wifiManager.getScanInfoList();
      Log.info(TAG, "scanInfoList:" + JSON.stringify(scanInfoList));
      let len = scanInfoList.length;
      Log.info(TAG, "wifi received scan info: " + len);
      if(len > 0){
        for (let i = 0; i < len; ++i) {
          Log.info(TAG, "ssid: " + scanInfoList[i].ssid);
          Log.info(TAG, "bssid: " + scanInfoList[i].bssid);
          Log.info(TAG, "capabilities: " + scanInfoList[i].capabilities);
          Log.info(TAG, "securityType: " + scanInfoList[i].securityType);
          Log.info(TAG, "rssi: " + scanInfoList[i].rssi);
          Log.info(TAG, "band: " + scanInfoList[i].band);
          Log.info(TAG, "frequency: " + scanInfoList[i].frequency);
          Log.info(TAG, "channelWidth: " + scanInfoList[i].channelWidth);
          Log.info(TAG, "timestamp: " + scanInfoList[i].timestamp);
        }
      }
    }catch(error){
      Log.info(TAG, "getScanInfoList failed:" + JSON.stringify(error))
    }
  }
}